package com.xuecheng.content.service.impl;

import cn.hutool.core.util.StrUtil;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.BindTeachplanMediaDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.domain.TeachplanMedia;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanMediaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.media.api.MediaClient;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-12-03
 */
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {

    @Autowired
    private TeachplanService teachplanService;


    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private MediaClient mediaClient;
    // 计划绑定媒资
    @Override
    public ResponseResult association(BindTeachplanMediaDto dto) {
        // 1.参数校验
        if (dto.getMediaId() == null || dto.getTeachplanId()==null) {
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        // 2.判断课程状态
        // 2-1 先查询课程计划
        Teachplan teachplan = teachplanService.getById(dto.getTeachplanId());
        // 2-2 查询课程信息
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        // 2-3 校验状态
        if (StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_COMMIT_STATUS)
                || StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PASTED_STATUS)
                ||  StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.MEDIABINDERROR);
        }
        // 3.feign远程调用
        ResponseResult<MediaVo> responseResult = mediaClient.findById(dto.getMediaId());
        // 3-1 判断是否调用成功
        if (responseResult.getCode() != 0) {
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        // 3-2 取出媒资对象
        MediaVo mediaVo = responseResult.getData();
        // 4.封装媒资绑定实体
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setMediaId(mediaVo.getId());//媒资ID
        teachplanMedia.setTeachplanId(teachplan.getId());//计划id
        teachplanMedia.setCourseId(courseBase.getId());//课程id
        teachplanMedia.setMediaFilename(mediaVo.getFilename());//媒资名称
        teachplanMedia.setCreateDate(new Date());//创建时间
        teachplanMedia.setChangePeople(AuthInfoHolder.getUserId().toString());//创建人

        //5.保存数据库
        this.save(teachplanMedia);
        // 6.返回ok
        return ResponseResult.okResult();
    }


    //媒资解绑
    @Override
    public ResponseResult deleteTeachPlanMedia(Long teachPlanMediaId) {
        //1.校验参数
        if (teachPlanMediaId == null) {
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        //2.查询媒资
        TeachplanMedia teachPlanMedia = this.getById(teachPlanMediaId);
        CourseBase courseBase = courseBaseService.getById(teachPlanMedia.getCourseId());
        //3.校验状态
        if (StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_COMMIT_STATUS)
                || StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PASTED_STATUS)
                ||  StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.MEDIABINDERROR);
        }
        //4.删除数据库
        this.removeById(teachPlanMediaId);
        // 5.返回ok
        return ResponseResult.okResult();
    }
}
